package com.chen.controller;


import com.chen.pojo.Department;
import com.chen.pojo.Employee;
import com.chen.service.DepartmentService;
import com.chen.service.EmployeeService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/all")
    public String toList(Model model){

        List<Employee> employees = employeeService.getAll();
        model.addAttribute("emp",employees);
        return "admin/list";
    }

    //添加员工页面
    @RequestMapping("/toAdd")
    public String toAdd(Model model){

        List<Department> departments = departmentService.allDept();

        //放入model
        model.addAttribute("dept",departments);
        return "admin/add";
    }


    //添加员工
    @RequestMapping("/add")
    public String addEmp(Employee employee){

        employeeService.addEmp(employee);

        return "redirect:/emp/all";
    }


    //删除员工
    @RequestMapping("/del/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
        employeeService.deleteEmp(id);
        return "redirect:/emp/all";
    }

    //去更新员工信息页面
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){

        Employee employee = employeeService.getById(id);
        List<Department> departments = departmentService.allDept();

        //放入model
        model.addAttribute("dept",departments);
        model.addAttribute("emp",employee);
        return "admin/update";
    }

    //更新员工
    @RequestMapping("/update")
    public String updateEmp(Employee employee){

        employeeService.updateEmp(employee);

        return "redirect:/emp/all";
    }
}
